from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time


def login(driver, phone, password):
    phone_input = driver.find_element(
        By.XPATH,
        "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[2]/uni-input/div/input",
    )
    password_input = driver.find_element(
        By.XPATH,
        "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[4]/uni-input/div/input",
    )
    login_button = driver.find_element(
        By.XPATH,
        "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[7]",
    )
    image_button = driver.find_element(
        By.XPATH,
        "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[6]/uni-image/img",
    )
    # 填写手机号 /html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[6]/uni-image/img
    phone_input.send_keys("19303065924")
    password_input.send_keys("asd1234567")

    image_button.click()
    time.sleep(2)
    login_button.click()


def sign_in(driver):
    # 模拟点击签到按钮
    # sign_button_xpath = "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[4]/uni-view/uni-image[2]/img"
    # sign_button = wait.until(EC.presence_of_element_located((By.XPATH, sign_button_xpath)))
    # sign_button.click()

    # # 等待签到成功的提示信息出现
    # success_message_xpath = "//div[contains(text(), '签到成功')]"
    # success_message = wait.until(EC.presence_of_element_located((By.XPATH, success_message_xpath)))
    # print("签到成功提示信息:", success_message.text)

    # 可以添加适当的等待时间以确保操作完成
    time.sleep(2)


def get_home_info(driver):
    wait = WebDriverWait(driver, 10)
    home_button_xpath = "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[8]/uni-view/uni-view[1]/uni-view[1]/uni-view[4]/uni-view/uni-image/img"
    home_button = wait.until(
        EC.presence_of_element_located((By.XPATH, home_button_xpath))
    )
    home_button.click()


def get_list_info(driver):
    wait = WebDriverWait(driver, 30)
    list_xpath = "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[8]/uni-view/uni-view[1]/uni-view[1]/uni-view[3]/uni-view/uni-image/img"

    try:
        list_button = wait.until(EC.presence_of_element_located((By.XPATH, list_xpath)))
        list_button.click()
    except TimeoutException:
        print("元素加载超时")


# 我的发布。
def get_my_list_info(driver):
    wait = WebDriverWait(driver, 30)
    list_xpath = "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[3]/uni-view[1]/uni-view[3]"

    try:
        list_button = wait.until(EC.presence_of_element_located((By.XPATH, list_xpath)))
        list_button.click()
    except TimeoutException:
        print("元素加载超时")


# 我的订单
def get_my_list_info(driver):
    wait = WebDriverWait(driver, 30)
    list_xpath = "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[3]/uni-view[1]/uni-view[3]"

    try:
        list_button = wait.until(EC.presence_of_element_located((By.XPATH, list_xpath)))
        list_button.click()
    except TimeoutException:
        print("元素加载超时")


def has_order_data(driver):
    try:
        # 尝试查找包含"暂无数据"的元素
        no_data_element = driver.find_element(By.CLASS_NAME, "view_is_nodata")
        # 如果找到了元素，说明没有订单数据
        return False
    except:
        # 如果未找到元素，说明有订单数据
        return True


def get_sell_data(driver):
    # /html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[1]/uni-view[2]
    wait = WebDriverWait(driver, 30)
    list_xpath = "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[1]/uni-view[2]"
    try:
        list_button = wait.until(EC.presence_of_element_located((By.XPATH, list_xpath)))
        list_button.click()
    except TimeoutException:
        print("元素加载超时")


def get_buy_sell(driver):
    wait = WebDriverWait(driver, 30)
    list_xpath = "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[1]/uni-view[1]"
    try:
        list_button = wait.until(EC.presence_of_element_located((By.XPATH, list_xpath)))
        list_button.click()
    except TimeoutException:
        print("元素加载超时")


# 设置浏览器驱动路径
edge_driver_path = "/home/huangxin/webdriver/msedgedriver"

# 创建 Edge 服务对象
edge_service = EdgeService(executable_path=edge_driver_path)

# 创建 Edge 浏览器对象，传递服务对象
driver = webdriver.Edge(service=edge_service)

# 打开登录页面
login_url = "https://www.aimatess.xyz/h5/#/pages/login/login"
driver.get(login_url)

# 等待页面加载完成，可以根据实际情况调整等待时间
time.sleep(2)

# 初始化 WebDriverWait
# wait = WebDriverWait(driver, 10)

# 调用登录函数
login(driver, "17620084608", "asd1234567")

# 使用 try-except 块处理 TimeoutException
try:
    get_list_info(driver)
except TimeoutException:
    print("元素加载超时1")

try:
    get_my_list_info(driver)
    time.sleep(10)
except TimeoutException:
    print("元素加载超时1")

while True:
    try:
        get_sell_data(driver)
        time.sleep(10)
        if has_order_data(driver):
            print("我的出售有订单")
        else:
            print("我的出售无订单")
    except TimeoutException:
        print("元素加载超时1")

    try:
        get_buy_sell(driver)
        time.sleep(10)
        if has_order_data(driver):
            print("我的购买有订单")
        else:
            print("我的购买无订单")
    except TimeoutException:
        print("元素加载超时")

    time.sleep(30)
# 调用签到函数
# sign_in(driver)

# 调用查询订单函数
# query_order(driver)

# 调用发布订单函数
# publish_order(driver)

# 关闭浏览器
# driver.quit()
